home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_126 / iconify / demo.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  419 lines

  1. /*  :ts=8 bk=0
  2.  *
  3.  * demo.c:    Demonstrates The Incredible New Iconification Program!!!!!!
  4.  *
  5.  * Leo L. Schwab            8712.10
  6.  */
  7. #include <exec/types.h>
  8. #include <intuition/intuition.h>
  9. #include "iconify.h"
  10.  
  11. #define    LENGTH        20
  12.  
  13. extern void    *OpenLibrary(), *OpenWindow(), *GetMsg();
  14. extern long    VBeamPos();
  15.  
  16.  
  17. /*  The Boing icon image  */
  18. UWORD icon[] = { 
  19.     0xFFFF,   0xFFFF,   0xFFFF,   0xC000, 
  20.     0x8000,   0x0,   0x0,   0x4000, 
  21.     0x8000,   0x0,   0x0,   0x4000, 
  22.     0x8000,   0x1DF7,   0x0,   0x4000, 
  23.     0x8001,   0xFBEF,   0xB000,   0x4000, 
  24.     0x800D,   0xF7DF,   0x7C00,   0x4000, 
  25.     0x803B,   0xEFBE,   0xFB80,   0x4000, 
  26.     0x8077,   0xDF7D,   0xF7C0,   0x4000, 
  27.     0x80EF,   0xBEFB,   0xEFA0,   0x4000, 
  28.     0x81DF,   0x7DF7,   0xDF70,   0x4000, 
  29.     0x83BE,   0xFBEF,   0xBEF8,   0x4000, 
  30.     0x837D,   0xF7DF,   0x7DF0,   0x4000, 
  31.     0x82FB,   0xEFBE,   0xFBE8,   0x4000, 
  32.     0x81F7,   0xDF7D,   0xF7D8,   0x4000, 
  33.     0x83EF,   0xBEFB,   0xEFB8,   0x4000, 
  34.     0x81DF,   0x7DF7,   0xDF70,   0x4000, 
  35.     0x80BE,   0xFBEF,   0xBEE0,   0x4000, 
  36.     0x807D,   0xF7DF,   0x7DC0,   0x4000, 
  37.     0x803B,   0xEFBE,   0xFB80,   0x4000, 
  38.     0x8007,   0xDF7D,   0xF600,   0x4000, 
  39.     0x8001,   0xBEFB,   0xF000,   0x4000, 
  40.     0x8000,   0x1DF7,   0x0,   0x4000, 
  41.     0x8000,   0x0,   0x0,   0x4000, 
  42.     0x8000,   0x0,   0x0,   0x4000, 
  43.     0xFFFF,   0xFFFF,   0xFFFF,   0xC000,
  44.  
  45.     0x0,   0x0,   0x0,   0x0, 
  46.     0x0,   0x0,   0x0,   0x0, 
  47.     0x0,   0x0,   0x0,   0x0, 
  48.     0x0,   0x1F0,   0x0,   0x0, 
  49.     0x1,   0xF860,   0x3000,   0x0, 
  50.     0xC,   0x301C,   0x7C00,   0x0, 
  51.     0x18,   0xE3E,   0x1800,   0x0, 
  52.     0x7,   0x1F0C,   0x700,   0x0, 
  53.     0xF,   0x8603,   0x8F80,   0x0, 
  54.     0x1C3,   0x1C7,   0xC300,   0x0, 
  55.     0x180,   0xE3E1,   0x80E0,   0x0, 
  56.     0x71,   0xF0C0,   0x71F0,   0x0, 
  57.     0xF8,   0x6038,   0xF860,   0x0, 
  58.     0x30,   0x1C7C,   0x3018,   0x0, 
  59.     0xE,   0x3E18,   0xE38,   0x0, 
  60.     0x11F,   0xC07,   0x1F00,   0x0, 
  61.     0x86,   0x38F,   0x8600,   0x0, 
  62.     0x1,   0xC7C3,   0x1C0,   0x0, 
  63.     0x3,   0xE180,   0xE380,   0x0, 
  64.     0x0,   0xC071,   0xF000,   0x0, 
  65.     0x0,   0x38F8,   0x7000,   0x0, 
  66.     0x0,   0x1C30,   0x0,   0x0, 
  67.     0x0,   0x0,   0x0,   0x0, 
  68.     0x0,   0x0,   0x0,   0x0, 
  69.     0x0,   0x0,   0x0,   0x0
  70. };
  71.  
  72. static struct Image iconimg = {        /*  Icon Image  */
  73.      0, 0,
  74.     50, 25, 2,
  75.     icon,
  76.     0x3, 0,
  77.     NULL
  78. };
  79.  
  80.  
  81. static WORD bordbox[] = {    /*  A box with an X in it  */
  82.     0, 0,
  83.     ICONWIDTH-1, 0,
  84.     ICONWIDTH-1, ICONHEIGHT-1,
  85.     0, ICONHEIGHT-1,
  86.     0, 0,
  87.     ICONWIDTH-1, ICONHEIGHT-1,
  88.     ICONWIDTH-1, 0,
  89.     0, ICONHEIGHT-1
  90. };
  91.  
  92. static struct Border iconbord = {    /*  Icon border  */
  93.      0, 0,
  94.     1, 0, JAM1,
  95.     8,
  96.     bordbox,
  97.     NULL,
  98. };
  99.  
  100.  
  101. /**************************************************************************
  102. * Gadgets for the application's window.
  103. */
  104.  
  105. static WORD box[] = {
  106.     -1, -1,
  107.     48, -1,
  108.     48, 10,
  109.     -1, 10,
  110.     -1, -1
  111. };
  112.  
  113. static struct Border bord = {    /*  Window gadget border  */
  114.      0, 0,
  115.     1, 0, JAM1,
  116.     5,
  117.     box,
  118.     NULL,
  119. };
  120.  
  121. static struct IntuiText text[] = {
  122.  {
  123.     1, 0, JAM1,
  124.     4, 1,
  125.     NULL,
  126.     (UBYTE *) "Image",
  127.     NULL,
  128.  }, {
  129.     1, 0, JAM1,
  130.     4, 1,
  131.     NULL,
  132.     (UBYTE *) "Bordr",
  133.     NULL,
  134.  }, {
  135.     1, 0, JAM1,
  136.     4, 1,
  137.     NULL,
  138.     (UBYTE *) "Funct",
  139.     NULL,
  140.  }
  141. };
  142.  
  143. static struct Gadget gads[] = {
  144.  {            /*  Image  */
  145.     &gads[1],
  146.     0, -10, 48, 10,
  147.     GADGHCOMP | GRELBOTTOM,
  148.     RELVERIFY | BOTTOMBORDER,
  149.     BOOLGADGET,
  150.     (APTR) &bord,
  151.     NULL,
  152.     &text[0],
  153.     NULL, NULL,
  154.     ICON_IMAGE, 0
  155.  }, {            /*  Border  */
  156.     &gads[2],
  157.     48, -10, 48, 10,
  158.     GADGHCOMP | GRELBOTTOM,
  159.     RELVERIFY | BOTTOMBORDER,
  160.     BOOLGADGET,
  161.     (APTR) &bord,
  162.     NULL,
  163.     &text[1],
  164.     NULL, NULL,
  165.     ICON_BORDER, 0
  166.  }, {            /*  Function  */
  167.     NULL,
  168.     96, -10, 48, 10,
  169.     GADGHCOMP | GRELBOTTOM,
  170.     RELVERIFY | BOTTOMBORDER,
  171.     BOOLGADGET,
  172.     (APTR) &bord,
  173.     NULL,
  174.     &text[2],
  175.     NULL, NULL,
  176.     ICON_FUNCTION, 0
  177.  }
  178. };
  179.  
  180. struct NewWindow windef = {
  181.     0, 0, 400, 100,
  182.     -1, -1,
  183.     CLOSEWINDOW | GADGETUP,
  184.     WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH | SMART_REFRESH |
  185.      NOCAREREFRESH | ACTIVATE,
  186.     gads,
  187.     NULL,
  188.     (UBYTE *) "Click on gadgets below and be amazed!",
  189.     NULL, NULL,
  190.     0, 0, 0, 0,
  191.     WBENCHSCREEN
  192. };
  193.  
  194. struct Window    *win;
  195. long        xa1[LENGTH], xa2[LENGTH], ya1[LENGTH], ya2[LENGTH];
  196. short        rnd();                /*  Forward reference  */
  197. void        iconfunc();            /*  Forward reference  */
  198. void        *IntuitionBase, *GfxBase;
  199.  
  200.  
  201. main ()
  202. {
  203.     register struct IntuiMessage    *msg;
  204.     register struct Gadget        *gad;
  205.     long                class;
  206.     UWORD                x = 0, y = 0;
  207.     int                type;
  208.     void                *ptr;
  209.  
  210.     openstuff ();
  211.     rnd ((int) -VBeamPos());
  212.     doline (win, 1);
  213.  
  214.     while (1) {
  215.         if (msg = GetMsg (win -> UserPort)) {
  216.             class = msg -> Class;
  217.             gad = (struct Gadget *) msg -> IAddress;
  218.             ReplyMsg (msg);
  219.  
  220.             if (class == CLOSEWINDOW)
  221.                 break;
  222.             else {
  223.                 switch (gad -> GadgetID) {
  224.                 case ICON_IMAGE:
  225.                     ptr = &iconimg;
  226.                     break;
  227.  
  228.                 case ICON_BORDER:
  229.                     ptr = &iconbord;
  230.                     break;
  231.  
  232.                 case ICON_FUNCTION:
  233.                     ptr = (void *) iconfunc;
  234.                     break;
  235.                 }
  236.  
  237.                 /*
  238.                  * And now!  The moment you've been waiting
  239.                  * for!
  240.                  */
  241.                 windef.LeftEdge = win -> LeftEdge;
  242.                 windef.TopEdge    = win -> TopEdge;
  243.                 CloseWindow (win);  win = NULL;
  244.  
  245.                 iconify (&x, &y, ICONWIDTH, ICONHEIGHT,
  246.                      NULL, ptr, (int) gad -> GadgetID);
  247.  
  248.                 if (!(win = OpenWindow (&windef)))
  249.                     die ("Window re-open failed\n");
  250.  
  251.                 doline (win, 1);
  252.             }
  253.         }
  254.         doline (win, 0);
  255.         WaitTOF ();
  256.     }
  257.     closestuff ();
  258. }
  259.  
  260. doline (win, reset)
  261. struct Window *win;
  262. int reset;
  263. {
  264.     register struct RastPort    *rp = win -> RPort;
  265.     static long            x1, x2, y1, y2, dx1, dx2, dy1, dy2;
  266.     static long            xmin, ymin, xmax, ymax;
  267.     static int            i, flag;
  268.  
  269.     if (reset) {
  270.         xmin    = win -> BorderLeft;
  271.         ymin    = win -> BorderTop;
  272.         xmax    = win->Width - win->BorderRight - 1;
  273.         ymax    = win->Height - win->BorderBottom - 1;
  274.         x1 = rnd ((int) xmax);  y1 = rnd ((int) ymax);
  275.         x2 = rnd ((int) xmax);  y2 = rnd ((int) ymax);
  276.         setdisp (&dx1, &dy1);
  277.         setdisp (&dx2, &dy2);
  278.         SetDrMd (rp, COMPLEMENT);
  279.         i = flag = 0;
  280.     }
  281.  
  282.     if (!rnd (20))
  283.         if (rnd (2))
  284.             setdisp (&dx1, &dy1);
  285.         else
  286.             setdisp (&dx2, &dy2);
  287.  
  288.     x1 += dx1;  y1 += dy1;
  289.     if (x1 > xmax || x1 < xmin) {
  290.         dx1 = -dx1;
  291.         x1 = x1<xmin ? xmin : xmax;
  292.     }
  293.     if (y1 > ymax || y1 < ymin) {
  294.         dy1 = -dy1;
  295.         y1 = y1<ymin ? ymin : ymax;
  296.     }
  297.  
  298.     x2 += dx2;  y2 += dy2;
  299.     if (x2 > xmax || x2 < xmin) {
  300.         dx2 = -dx2;
  301.         x2 = x2<xmin ? xmin : xmax;
  302.     }
  303.     if (y2 > ymax || y2 < ymin) {
  304.         dy2 = -dy2;
  305.         y2 = y2<ymin ? ymin : ymax;
  306.     }
  307.  
  308.     Move (rp, x1, y1);
  309.     Draw (rp, x2, y2);
  310.     if (flag) {
  311.         Move (rp, xa1[i], ya1[i]);
  312.         Draw (rp, xa2[i], ya2[i]);
  313.     }
  314.     xa1[i] = x1;  ya1[i] = y1;
  315.     xa2[i] = x2;  ya2[i] = y2;
  316.  
  317.     if (++i >= LENGTH) {
  318.         i = 0;
  319.         flag = 1;
  320.     }
  321. }
  322.  
  323. setdisp (x, y)
  324. register long *x, *y;
  325. {
  326.     *x = rnd (9) - 4;
  327.     *y = rnd (9) - 4;
  328. }
  329.  
  330. void
  331. iconfunc (win, init)
  332. struct Window *win;
  333. WORD init;
  334. {
  335.     if (init) {
  336.         /*  Initialization pass, draw a border in the icon  */
  337.         SetAPen (win -> RPort, 1L);
  338.         Move (win -> RPort, 0L, 0L);
  339.         Draw (win -> RPort, win -> Width - 1L, 0L);
  340.         Draw (win -> RPort, win -> Width - 1L, win -> Height - 1L);
  341.         Draw (win -> RPort, 0L, win -> Height - 1L);
  342.         Draw (win -> RPort, 0L, 0L);
  343.         doline (win, 1);
  344.     }
  345.     doline (win, 0);
  346. }
  347.  
  348. openstuff ()
  349. {
  350.     if (!(IntuitionBase = OpenLibrary ("intuition.library", 0L)))
  351.         die ("-=RJ=-'s missing.\n");
  352.  
  353.     if (!(GfxBase = OpenLibrary ("graphics.library", 0L)))
  354.         die ("Dale, where are you?\n");
  355.  
  356.     if (!(win = OpenWindow (&windef)))
  357.         die ("Window painted shut.\n");
  358. }
  359.  
  360. closestuff ()
  361. {
  362.     if (win)        CloseWindow (win);
  363.     if (GfxBase)        CloseLibrary (GfxBase);
  364.     if (IntuitionBase)    CloseLibrary (IntuitionBase);
  365. }
  366.  
  367. die (str)
  368. char *str;
  369. {
  370.     extern long    Output();
  371.  
  372.     Write (Output (), str, (long) strlen (str));
  373.     closestuff ();
  374.     exit (20);
  375. }
  376.  
  377. /***********************************************************************/
  378. #asm
  379. *\
  380. *  :ts=8
  381. * Yet Another random number generator.  By Leo Schwab.
  382. * Based on an idea posted on the USENET (Thanks, Sam Dicker!)
  383. * For the Manx assembler.
  384. *
  385. * Calling convention:
  386. *  short rnd (range);
  387. *  short range;
  388. *
  389. * 8606.30
  390. */
  391.  
  392.         public    _rnd
  393.  
  394. _rnd        lea    rndseed,a0    Get address of seed
  395.         move.w    4(sp),d1    Get range argument
  396.         tst.w    d1
  397.         ble.s    setseed        Go reset seed
  398.  
  399.  
  400.         move.l    (a0),d0        Get seed
  401.         ADD.L   D0,D0
  402.         BHI.S   over
  403.         EORI.L  #$1D872B41,D0
  404. over
  405.         move.l    d0,(a0)        Save new seed
  406.         andi.l    #$ffff,d0    Coerce into word
  407.         divu    d1,d0        Divide by range
  408.         swap    d0         and get remainder (modulus)
  409.         rts
  410.  
  411. setseed        neg.w    d1        Probably don't need this
  412.         move.l    d1,(a0)
  413.         rts
  414.  
  415.         dseg
  416. rndseed        dc.l    0
  417.         cseg
  418. #endasm
  419.